package com.cgly.medical.service.impl;

import com.cgly.medical.domain.DrugCompany;
import com.cgly.medical.entity.CompanyPolicyEntity;
import com.cgly.medical.mapper.CompanyPolicyMapper;
import com.cgly.medical.mapper.DrugCompanyMapper;
import com.cgly.medical.model.CompanyPolicyModel;
import com.cgly.medical.model.DrugCompanyModel;
import com.cgly.medical.param.CompanyPolicyParam;
import com.cgly.medical.service.CompanyPolicyService;
import com.cgly.medical.util.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 黄浩然
 */

@Service
public class CompanyPolicyServiceImpl implements CompanyPolicyService {

    @Autowired
    private CompanyPolicyMapper companyPolicyMapper;
    @Autowired
    private DrugCompanyMapper drugCompanyMapper;

//    @Cacheable(value = "CompanyPolicy", key = "'getAllPolicyWithPage'")
    @Override
    public Msg getAllPolicyWithPage(CompanyPolicyParam param) {
        if (param.getSize() == 0) {
            param.setSize(1);
        }
        PageHelper.startPage(param.getPn(), param.getSize());
        List<CompanyPolicyModel> list = companyPolicyMapper.getAllCompanyPolicy(param);
        PageInfo<CompanyPolicyModel> info = new PageInfo<>(list, 5);

        return Msg.success().data("companypolicy",info);
    }

    @Override
    public Msg saveCompanyPolicy(CompanyPolicyParam param) {
        param.setCreateTime(new DateTime().toDate());
        param.setUpdateTime(new DateTime().toDate());
        CompanyPolicyEntity entity = new CompanyPolicyEntity();
        BeanUtils.copyProperties(param, entity);
        int i = companyPolicyMapper.saveCompanyPolicy(entity);
        List<CompanyPolicyModel> allCompanyPolicy = companyPolicyMapper.getAllCompanyPolicy(null);
        entity.setTotal((long) allCompanyPolicy.size());
//        List<DrugCompany> drugCompanyModels = drugCompanyMapper.selectAllDrugCompany();
        if (i > 0) {
            Long num = entity.getTotal() % 5 == 0 ? (entity.getTotal() / 5) : (entity.getTotal() / 5) + 1;
            return Msg.success().mess("添加成功").data("numberOfAdd", i).data("pages", num);
        }
        return Msg.fail().mess("添加失败");
    }

    @Override
    public Msg getCompanyPolicyById(Long id) {

        CompanyPolicyModel companyPolicyModel = companyPolicyMapper.getCompanyPolicyById(id);
        List<DrugCompany> drugCompanyModels = drugCompanyMapper.selectAllDrugCompany();

        return Msg.success().data("companypolicy",companyPolicyModel).data("drugCompanyModels",drugCompanyModels);
    }

    @Override
    public Msg updateCompanyPolicy(Long id,CompanyPolicyParam param) {
        param.setUpdateTime(new DateTime().toDate());
        CompanyPolicyEntity entity = new CompanyPolicyEntity();
        BeanUtils.copyProperties(param, entity);
        entity.setId(id);
        int i = companyPolicyMapper.updateCompanyPolicy(entity);
        if (i > 0) {
            return Msg.success().mess("修改成功").data("updateData", entity);
        }
        return Msg.fail().mess("修改失败");
    }

    @Override
    public Msg deleteCompanyPolicy(Long id) {
        int i = companyPolicyMapper.deleteCompanyPolicy(id);
        if (i > 0) {
            return Msg.success().mess("删除成功").data("numberOfDelete", i);
        }
        return Msg.fail().mess("删除失败");
    }
}
